import {Component, OnInit, Output, EventEmitter} from '@angular/core';
import {FormGroup, FormControl, Validators, FormBuilder} from "@angular/forms";

@Component({
  selector: 'app-user-register',
  templateUrl: './user-register.component.html',
  styleUrls: ['./user-register.component.scss']
})
export class UserRegisterComponent implements OnInit {
  registerFrom: FormGroup;

  @Output() loginEvent:EventEmitter<any> = new EventEmitter();

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.registerFrom = this.fb.group({
      email: [null, [Validators.email, Validators.required]],
      password: [null, [Validators.required]],
      checkPassword: [null, [Validators.required, this.confirmationValidator]],
      nickname: [null, [Validators.required]],
      phoneNumber: [null, [Validators.required]],
      captcha: [null, [Validators.required]],
    })
  }


  submitRegister(value): void {
    console.log(value);
  }

  updateConfirmValidator(){
    Promise.resolve().then(() => this.registerFrom.controls.checkPassword.updateValueAndValidity());
  }

  confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.registerFrom.controls.password.value) {
      return { confirm: true, error: true };
    }
    return {};
  };

  getCaptcha(event){
    console.log(event)
  }

  toLogin(){
    this.loginEvent.emit();
  }



}
